﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace myfirstmaui.ViewModels
{
    public class MyPage66ViewModel : ObservableValidator    
    {
        private int? age;

        [Required(ErrorMessage = "不能为空!")]
        [Range(18,120,ErrorMessage = "必须是18和120之间")]
        public int? Age
        {
            get { return age; }
            //第3个参数是必须校验的..
            set { SetProperty(ref age, value,true); }
        }

        private bool active1;
        public bool Active1
        {
            get { return active1; }
            set
            {                
                SetProperty(ref active1, value);
                ImageSource1 = Active1 ? "dotnet_bot.png" : "dotnet_bot2.png";
            } //第2句无意义，可是必须又要除服..
        }

        private string imageSource1;
        public string ImageSource1 {get { return imageSource1;} set { SetProperty(ref imageSource1, value);}}


        public ICommand BtnCommandSendHand { get; set; } //

        string? errorMessages;
        public string? ErrorMessages
        {
            get { return errorMessages; }            
            set { SetProperty(ref errorMessages, value); }
        }

        public MyPage66ViewModel()
        {

            BtnCommandSendHand = new RelayCommand<object>(DoBtnCommandSendHand);
        }

        private void DoBtnCommandSendHand(object button)
        {

            Active1 = !Active1;

            ValidateAllProperties();

            if (HasErrors)
            {
                ErrorMessages = string.Join(Environment.NewLine, GetErrors());
                return;
            }

            ErrorMessages = "";

            return;
        }
    }
}
